getCommentList();
//获取评论数据
function getCommentList() {
  $.ajax({
    type: "GET",
    url: "http://localhost:3000/comments",
    dataType: "json",
    success: (data) => {
      //   console.log(data);
      $(".list").html(template("comments", { comments: data }));
      deleteComment();
    },
  });
}
// 删除评论
function deleteComment() {
  $(".cmts .list .floor .delete").click(function () {
    let id = $(this).attr("data-id");
    let li = $(this).parent("li");
    // console.log(id);
    $.ajax({
      type: "DELETE",
      url: "http://localhost:3000/comments/" + id,
      success: () => {
        debugger;
        li.remove();
      },
    });
  });
}
//发表评论
$(".btn").click(function () {
  let userName = $(".user").val();
  let content = $(".editor").val();
  if (userName === "" || content === "") {
    alert("请输入您的昵称或发言");
    return;
  }
  $.ajax({
    type: "POST",
    url: "http://localhost:3000/comments",
    data: {
      username: userName,
      content: content,
    },
    success: (data) => {
      $(".list").append("comments", { comments: data });
    },
  });
});
